package com.yanzhu.api;

import com.yanzhu.bean.ResponseBean;
import com.yanzhu.service.MaterialService;
import com.yanzhu.service.TempletService;
import com.yanzhu.service.model.Templet;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 方炎柱
 * @Description:
 * @Date: Created in 20:18 2018/1/31
 * @Modified:
 */
@AllArgsConstructor
@RestController
public class TempletApi {
    private final TempletService templetService;

    private final MaterialService materialService;

    @RequestMapping(value="/queryTempletList")
    public ResponseBean queryTempletList() throws Exception {

        return ResponseBean.ok(templetService.queryTempletList());
    }

    @RequestMapping(value="/queryTempletById")
    public ResponseBean queryTempletById(HttpServletRequest request) throws Exception {

        String tid = request.getParameter("tid");
        if(tid == null || tid.equals(""))
            return ResponseBean.fail("缺少模版ID参数!");
        Templet templet = templetService.queryTempletById(Long.parseLong(tid));
        if(templet == null)
            return ResponseBean.fail("该模版不存在!");
        templet.setMaterials(materialService.queryMaterialListByTid(Long.parseLong(tid)));
        return ResponseBean.ok(templet);
    }
}
